<?php
session_start();

include ("../includes/connection.php");

if (isset($_SESSION{"logged_in"})) {
    //display add page
    if(isset($_POST['title'], $_POST["content"] )){
        $title = $_POST[ "title" ];
        $content = $_POST["content"];

        if(empty($title) or empty($content)){
            $error = "All fileds are required!";
        } else{
            $query = $pdo->prepare("INSERT INTO articles (article_title, article_content, article_timestamp ) VALUES (?, ?, ?)");

            $query->bindValue(1, $title);
            $query->bindValue(2, $content);
            $query->bindValue(3, time());

            $query->execute();

            header("Location: index.php");
        }
    
    }

?>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>CMS</title>
            <link rel="stylesheet" href="../assets/style.css" media="all">
        </head>
        <body>
            <div class="container">
                <a id="logo" href="index.php">CMS</a>

                <br>
                
                <h4>Add article</h4>

                <?php if (isset($error)) {?>
                    <small style="color=#aa0000"> <?php echo $error ?></small>
                <br>
                <br>
                <?php } ?>

                <form method="post" action="add.php" autocompltete="off">
                    <input id="" type="text" name="title" placeholder="Title"> <br><br>
                    <textarea id="" name="content" placeholder="Content" cols="50" rows="15"></textarea> 
                        <br>
                        <br>
                    <input type="submit" value="Add Article">
                </form>

            </div>
        </body>
    
<?php
} else {

    header("Location: index.php");
}
?>
